﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Nha_Hang_Khach_San.NHKSServiceReference;
using System.ServiceModel;
using System.Windows.Media.Imaging;
using System.IO;

namespace Nha_Hang_Khach_San.Khach_San
{
    public partial class CTPhong : ChildWindow
    {
        string tenPhong = "";
        List<ImgList> imgListPhongs=new List<ImgList>();
        int totaImage = 0;
        int indexImage = 0;
        //
        public CTPhong(string _tenPhong)
        {
            InitializeComponent();
            tenPhong = _tenPhong;
            this.Title = "Chi tiết phòng " + tenPhong;
            //dataGrid1.Loaded += new RoutedEventHandler(dataGrid1_Loaded);
            NHKSServiceClient client = new NHKSServiceClient();
            client.getFirstImageCompleted += new EventHandler<getFirstImageCompletedEventArgs>(client_getFirstImageCompleted);
            client.getFirstImageAsync(tenPhong);
        }

        void client_getFirstImageCompleted(object sender, getFirstImageCompletedEventArgs e)
        {
            string fileName = e.Result;
            NHKSServiceClient client = new NHKSServiceClient();
            client.DowloadCompleted+=new EventHandler<DowloadCompletedEventArgs>(client_DowloadCompleted);
            client.DowloadAsync(fileName);
        }

        public void GetListImage()
        {
            NHKSServiceClient client = new NHKSServiceClient();
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress end = new EndpointAddress("http://localhost:3418/NHKSService.svc?wsdl");
            client.GetDSHinhAnhCompleted+=new EventHandler<GetDSHinhAnhCompletedEventArgs>(client_GetDSHinhAnhCompleted);
            client.GetDSHinhAnhAsync(tenPhong);
        }

        //void imgPhong_Loaded(object sender, RoutedEventArgs e)
        //{
        //    string fileName = "";
        //    NHKSServiceClient client = new NHKSServiceClient();
        //    BasicHttpBinding bind = new BasicHttpBinding();
        //    EndpointAddress end = new EndpointAddress("http://localhost:2917/NHKSService.svc?wsdl");
        //    foreach (var temp in imgListPhongs)
        //    {
        //        if(indexImage==temp.ImgIndex)
        //        {
        //            fileName = temp.ImgUrl;
        //            indexImage++;
        //        }
        //        break;
        //    }
        //    client.DowloadCompleted += new EventHandler<DowloadCompletedEventArgs>(client_DowloadCompleted);
        //    client.DowloadAsync(fileName);
        //}

        void client_DowloadCompleted(object sender, DowloadCompletedEventArgs e)
        {
            BitmapImage bmImage = new BitmapImage();
            if(e.Error==null)
            {
                if(e.Result!=null)
                {
                    PictureFile picture = e.Result;
                    MemoryStream stream = new MemoryStream(picture.PictureStream);
                    bmImage.SetSource(stream);
                    imgPhong.Source = bmImage;
                }
            }
        }

        //void dataGrid1_Loaded(object sender, RoutedEventArgs e)
        //{
        //    NHKSServiceClient client = new NHKSServiceClient();
        //    BasicHttpBinding bind = new BasicHttpBinding();
        //    EndpointAddress end = new EndpointAddress("http://localhost:2917/NHKSService.svc?wsdl");
        //    client.GetDSHinhAnhCompleted += new EventHandler<GetDSHinhAnhCompletedEventArgs>(client_GetDSHinhAnhCompleted);
        //    client.GetDSHinhAnhAsync(tenPhong);
        //}

        void client_GetDSHinhAnhCompleted(object sender, GetDSHinhAnhCompletedEventArgs e)
        {
            //dataGrid1.ItemsSource = e.Result;
            foreach (var temp in e.Result)
            {
                totaImage++;
                ImgList imgListPhong = new ImgList();
                imgListPhong.ImgIndex = temp.ImgIndex;
                imgListPhong.ImgUrl = temp.ImgUrl;
                imgListPhongs.Add(imgListPhong);
            }
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            btnPrevious.IsEnabled = false;
            NHKSServiceClient client = new NHKSServiceClient();
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress end = new EndpointAddress("http://localhost:3418/NHKSService.svc?wsdl");
            client.GetDSHinhAnhCompleted += new EventHandler<GetDSHinhAnhCompletedEventArgs>(client_GetDSHinhAnhCompleted);
            client.GetDSHinhAnhAsync(tenPhong);
            //
            client.GetLoaiPhongCompleted += new EventHandler<GetLoaiPhongCompletedEventArgs>(client_GetLoaiPhongCompleted);
            client.GetLoaiPhongAsync(tenPhong);
            //
            client.GetNhomPhongCompleted += new EventHandler<GetNhomPhongCompletedEventArgs>(client_GetNhomPhongCompleted);
            client.GetNhomPhongAsync(tenPhong);
            //
            client.GetDonGiaPhongCompleted += new EventHandler<GetDonGiaPhongCompletedEventArgs>(client_GetDonGiaPhongCompleted);
            client.GetDonGiaPhongAsync(tenPhong);
            //
            client.GetMoTaPhongCompleted += new EventHandler<GetMoTaPhongCompletedEventArgs>(client_GetMoTaPhongCompleted);
            client.GetMoTaPhongAsync(tenPhong);
        }

        void client_GetDonGiaPhongCompleted(object sender, GetDonGiaPhongCompletedEventArgs e)
        {
            tblDonGia.Text = "Đơn giá: "+e.Result + " VND";
        }

        void client_GetNhomPhongCompleted(object sender, GetNhomPhongCompletedEventArgs e)
        {
            tblNhomPhong.Text = "Nhóm phòng: " + e.Result;
        }

        void client_GetLoaiPhongCompleted(object sender, GetLoaiPhongCompletedEventArgs e)
        {
            tblLoaiPhong.Text = "Loại phòng: " + e.Result;
        }

        void client_GetMoTaPhongCompleted(object sender, GetMoTaPhongCompletedEventArgs e)
        {
            txbMoTa.Text = "Mô tả: " + e.Result;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "";
            NHKSServiceClient client = new NHKSServiceClient();
            indexImage = indexImage + 1;
            btnPrevious.IsEnabled = true;
            foreach (var temp in imgListPhongs)
            {
                if (indexImage == totaImage - 1)
                {
                    btnNext.IsEnabled = false;
                }
                if (indexImage == temp.ImgIndex)
                {
                    fileName = temp.ImgUrl;
                    break;
                }
            }          
            client.DowloadCompleted += new EventHandler<DowloadCompletedEventArgs>(client_DowloadCompleted);
            client.DowloadAsync(fileName);
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "";
            NHKSServiceClient client = new NHKSServiceClient();
            indexImage = indexImage - 1;
            btnNext.IsEnabled = true;
            foreach (var temp in imgListPhongs)
            {
                if(indexImage==0)
                {
                    btnPrevious.IsEnabled = false;
                }
                if (indexImage == temp.ImgIndex)
                {
                    fileName = temp.ImgUrl;
                    break;
                }
            }
            client.DowloadCompleted += new EventHandler<DowloadCompletedEventArgs>(client_DowloadCompleted);
            client.DowloadAsync(fileName);
        }
    }
}

